package accountws;

import branchws.DBHandler;
import java.util.List;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

/**
 *
 * @author Atuan
 */
@WebService(serviceName = "AccountWS")
public class AccountWS {

    IAccountDB dbh = new DBHandler();
    
    @WebMethod(operationName = "login")
    public LoginReply login(String username, String password){
        System.out.println("login checks");
        return dbh.login(username, password);
    }
    
    @WebMethod(operationName = "logout")
    public Boolean logout(String username){
        System.out.println("logout checks");
        return dbh.logout(username);
    }
    
    @WebMethod(operationName = "adduser")
    public Boolean addUser(
            @WebParam(name = "username") String username, 
            @WebParam(name = "password") String password, 
            @WebParam(name = "role") String role,
            @WebParam(name = "sessionid") String sid)
                throws SessionExpiredException {
        if(dbh.checkUser(sid)){
            return dbh.addUser(username, password, role);
        } else {
            throw new SessionExpiredException();
        }
    }
//    
//    @WebMethod(operationName = "register")
//    public Boolean register(@WebParam(name = "username") String username, 
//        @WebParam(name = "password") String password, @WebParam(name = "role") String role){
//        System.out.println("register");
//        return dbh.addUser(username, password, role);        
//    }
//    
//    @WebMethod(operationName = "register2")
//    public Boolean register(@WebParam(name = "username") String username, 
//        @WebParam(name = "password") String password){
//        System.out.println("register");
//        return dbh.addUser(username, password);        
//    }
    @WebMethod(operationName = "removeuser")
    public Boolean removeUser(@WebParam(name = "username") String username, String sid)
        throws SessionExpiredException {
        System.out.println("remove user: "+username);
        if(dbh.checkUser(sid)){
            return dbh.removeUser(username);
        } else {
            throw new SessionExpiredException();
        }        
    }
    
    @WebMethod(operationName = "getallusers")
    public List<User> getAllUsers(String sid)
        throws SessionExpiredException {
        if(dbh.checkUser(sid)){
            return dbh.getAllUsers();
        } else {
            throw new SessionExpiredException();
        }
    }
    
    @WebMethod(operationName = "test")
    public void test(){
        LoginReply lr = login("admin", "admin");
        System.out.println("loginreply: "+lr.getRole()
                +" i "+lr.getSessionID());         
    }
    /*
    @WebMethod(operationName = "test")
    public Boolean test()
        throws SessionExpiredException {
        
        LoginReply lr = login("admin", "admin");
        System.out.println("loginreply: "+lr.getRole()+" i "+lr.getSessionID());
        List<User> temp1 = getAllUsers(lr.getSessionID());
        for(int i = 0; i < temp1.size(); i++){
            System.out.println(temp1.get(i).getUsername()+" "+temp1.get(i).getPassword()+" "+temp1.get(i).getRole());
        }
        System.out.println("logout "+logout("admin"));
        
        lr = login("admin", "admin");
        System.out.println("loginreply: "+lr.getRole()+" i "+lr.getSessionID());        
        System.out.println("removeuser "+removeUser("NJ",lr.getSessionID()));       
        System.out.println("wrong sessionID:");
        try{
            temp1 = getAllUsers("wdfrgbynyhafejgfrjv9");
            for(int i = 0; i < temp1.size(); i++){
                System.out.println(temp1.get(i).getUsername()
                        +" "+temp1.get(i).getPassword()+" "
                        +temp1.get(i).getRole());
            }
        } catch(SessionExpiredException e){
            System.out.println("error: "+e.getMessage());
        }
        System.out.println("addUser "+addUser("mania","mania",
                "admin",lr.getSessionID()));
//        System.out.println("register "+register("test","test"));
//        System.out.println("register "+register("alcatraz","abc"));
//        System.out.println("register "+register("csi","csi"));
//        System.out.println("register "+register("NJ","newalbum"));
        temp1 = getAllUsers(lr.getSessionID());
        for(int i = 0; i < temp1.size(); i++){
            System.out.println(temp1.get(i).getUsername()+" "+temp1.get(i).getPassword()+" "+temp1.get(i).getRole());
        }
        System.out.println("logout "+logout("NJ"));
        System.out.println("login "+login("NJ","bad"));
        return true;
    }
    //*/
}
